﻿Imports System.Xml

Public Class FormMain


#Region "Events"

    Private Sub FormMain_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load

        TxtFile1FilePath.Text = My.Settings.File1_Path
        TxtFile2FilePath.Text = My.Settings.File2_Path
        TxtFile1FriendlyName.Text = My.Settings.File1_FriendlyName
        TxtFile2FriendlyName.Text = My.Settings.File2_FriendlyName

    End Sub

    Private Sub BtnFile1FilePathBrowse_Click(sender As System.Object, e As System.EventArgs) Handles BtnFile1FilePathBrowse.Click

        PutFileNameFromDialogIntoTextBox(TxtFile1FilePath)

    End Sub

    Private Sub BtnFile2FilePathBrowse_Click(sender As System.Object, e As System.EventArgs) Handles BtnFile2FilePathBrowse.Click

        PutFileNameFromDialogIntoTextBox(TxtFile2FilePath)

    End Sub

    Private Sub BtnDoCompare_Click(sender As System.Object, e As System.EventArgs) Handles BtnDoCompare.Click

        If CheckAllInputsExistAndHighlightIfNot() Then

            ResetResultAndInputBoxes()

            SaveInputsToUserSettings()

            Dim file1Keys As SortedList(Of String, String) = GetAllKeysAndReportDuplicateKeys(My.Settings.File1_Path, My.Settings.File1_FriendlyName)
            Dim file2Keys As SortedList(Of String, String) = GetAllKeysAndReportDuplicateKeys(My.Settings.File2_Path, My.Settings.File2_FriendlyName)

            For Each key As String In file1Keys.Keys

                If Not file2Keys.ContainsKey(key) Then

                    ReportKeyDifference(My.Resources.Report_KeyNotInBothFiles,
                                        key,
                                        My.Settings.File1_FriendlyName,
                                        My.Settings.File2_FriendlyName)

                End If

            Next


            For Each key As String In file2Keys.Keys

                If Not file1Keys.ContainsKey(key) Then

                    ReportKeyDifference(My.Resources.Report_KeyNotInBothFiles,
                                        key,
                                        My.Settings.File2_FriendlyName,
                                        My.Settings.File1_FriendlyName)

                End If

            Next


            For Each key As String In file1Keys.Keys

                If file2Keys.ContainsKey(key) AndAlso Not file1Keys(key).Equals(file2Keys(key)) Then

                    ReportValueDifference(My.Resources.Report_ValueDifferentInFiles,
                                          key,
                                          file1Keys(key),
                                          My.Settings.File1_FriendlyName,
                                          file2Keys(key),
                                          My.Settings.File2_FriendlyName)

                End If

            Next

        Else

            MessageBox.Show(My.Resources.MissingInputs_ErrorMessage,
                            My.Resources.MissingInputs_ErrorMessageTitle,
                            MessageBoxButtons.OK,
                            MessageBoxIcon.Exclamation)

        End If

    End Sub

    Private Sub TxtFile1FriendlyName_GotFocus(sender As Object, e As System.EventArgs) Handles TxtFile1FriendlyName.GotFocus
        TxtFile1FriendlyName.BackColor = SystemColors.Window
    End Sub

    Private Sub TxtFile2FriendlyName_GotFocus(sender As Object, e As System.EventArgs) Handles TxtFile2FriendlyName.GotFocus
        TxtFile2FriendlyName.BackColor = SystemColors.Window
    End Sub

    Private Sub TxtFile1FilePath_GotFocus(sender As Object, e As System.EventArgs) Handles TxtFile1FilePath.GotFocus
        TxtFile1FilePath.BackColor = SystemColors.Window
    End Sub

    Private Sub TxtFile2FilePath_GotFocus(sender As Object, e As System.EventArgs) Handles TxtFile2FilePath.GotFocus
        TxtFile2FilePath.BackColor = SystemColors.Window
    End Sub

#End Region


#Region "Private Methods"

    Private Sub PutFileNameFromDialogIntoTextBox(ByRef textBoxToPutResultInto As TextBox)

        Dim openFileResult As DialogResult = ofdConfigurationFiles.ShowDialog()

        If openFileResult = Windows.Forms.DialogResult.OK Then
            textBoxToPutResultInto.Text = ofdConfigurationFiles.FileName
        End If

    End Sub

    Private Sub ResetResultAndInputBoxes()

        LstKeyDifferences.Items.Clear()
        LstValueDifferences.Items.Clear()

        TxtFile1FilePath.BackColor = SystemColors.Window
        TxtFile2FilePath.BackColor = SystemColors.Window
        TxtFile1FriendlyName.BackColor = SystemColors.Window
        TxtFile2FriendlyName.BackColor = SystemColors.Window

    End Sub

    Private Sub SaveInputsToUserSettings()

        My.Settings.File1_Path = TxtFile1FilePath.Text
        My.Settings.File2_Path = TxtFile2FilePath.Text
        My.Settings.File1_FriendlyName = TxtFile1FriendlyName.Text
        My.Settings.File2_FriendlyName = TxtFile2FriendlyName.Text

    End Sub

    Private Function CheckAllInputsExistAndHighlightIfNot() As Boolean

        Dim allinputsExist As Boolean = True

        If Not CheckTextBoxHasTextAndHighlightIfNot(TxtFile1FilePath) Then
            allinputsExist = False
        End If

        If Not CheckTextBoxHasTextAndHighlightIfNot(TxtFile2FilePath) Then
            allinputsExist = False
        End If

        If Not CheckTextBoxHasTextAndHighlightIfNot(TxtFile1FriendlyName) Then
            allinputsExist = False
        End If

        If Not CheckTextBoxHasTextAndHighlightIfNot(TxtFile2FriendlyName) Then
            allinputsExist = False
        End If

        Return allinputsExist

    End Function

    Private Function CheckTextBoxHasTextAndHighlightIfNot(ByRef textBoxToCheck As TextBox) As Boolean

        If String.IsNullOrWhiteSpace(textBoxToCheck.Text) Then

            textBoxToCheck.BackColor = Color.Red
            Return False

        End If

        Return True

    End Function

    Private Function GetAllKeysAndReportDuplicateKeys(filePath As String, fileFriendlyName As String) As SortedList(Of String, String)

        Dim distinctSortedKeys As New SortedList(Of String, String)

        Dim xmlDocumentBeingRead As New XmlDocument()
        xmlDocumentBeingRead.Load(filePath)

        Dim appSettingsNodeList As XmlNodeList = xmlDocumentBeingRead.SelectNodes("/appSettings/add")

        For Each currentKeyNode As XmlNode In appSettingsNodeList

            Dim keyAttrib As XmlAttribute = currentKeyNode.Attributes("key")
            Dim valueAttrib As XmlAttribute = currentKeyNode.Attributes("value")

            If distinctSortedKeys.Keys.Contains(keyAttrib.Value) Then
                ReportKeyDifference(My.Resources.Report_Duplicate_Keys, keyAttrib.Value, fileFriendlyName.ToString)
            Else
                distinctSortedKeys.Add(keyAttrib.Value, valueAttrib.Value)
            End If

        Next

        Return distinctSortedKeys

    End Function

    Private Sub ReportKeyDifference(message As String,
                                    Optional arg0 As String = Nothing,
                                    Optional arg1 As String = Nothing,
                                    Optional arg2 As String = Nothing,
                                    Optional arg3 As String = Nothing,
                                    Optional arg4 As String = Nothing)

        message = String.Format(message, arg0, arg1, arg2, arg3, arg4)
        LstKeyDifferences.Items.Add(message)

    End Sub

    Private Sub ReportValueDifference(message As String,
                                      Optional arg0 As String = Nothing,
                                      Optional arg1 As String = Nothing,
                                      Optional arg2 As String = Nothing,
                                      Optional arg3 As String = Nothing,
                                      Optional arg4 As String = Nothing)

        message = String.Format(message, arg0, arg1, arg2, arg3, arg4)
        LstValueDifferences.Items.Add(message)

    End Sub

#End Region


End Class
